package engine.ani;

import java.io.DataInputStream;
import java.io.InputStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import mainpackage.Constant;
import mainpackage.ResManager;
import mainpackage.Tools;

public class Animation {
    // 常量,表示几种数据占用的字节长度，读取动画文件时用
    final static int EXTEND_PER_ACTION = 2;
    final static int EXTEND_PER_MODULE = 5;
    final static int EXTEND_PER_SPRITE = 4;
    final static int EXTEND_PER_MECHMODULE = 4;
    final static int EXTEND_PER_COLBOX = 4;
    final static int EXTEND_PER_ATTBOX = 4;
    /*
     * Action人物动作 { {frameID, delay, frameID, delay, ......}, // Action 1
     * {frameID, delay, frameID, delay, ......}, // Action 2 ...... }
     *
     * frameID: 该桢在m_frames中的索引下标 delay：显示周期，单位按刷新次数计算
     */
    short m_actions[][];
    // 每个动作的桢数
    byte m_nFrames[];
    // 桢信息
    short m_frames[][];
    // 单元图信息
    short m_modules[];
    // 显示周期计时
    byte m_durCount;
    // 当前动作ID
    int m_actionID = -1;
    // 当前播放的是动作中的第几桢
    int m_pageID;
    // 当前动作是否放完一次，注意，当该动作播放到最后一桢后，会继续循环播放
    public boolean m_actionOver = true;
    // 要画的当前桢的id
    int m_frameIndex;
    //初始化完毕可以绘制
    public boolean blCanShow = false;
    public boolean blHaveAni = false;
    private String strAvatarId = null;
    public int iAnimationId = 0;
    public byte byAnimationType = 0;
    //当前动画文件匹配的图片
    private Image[] imgAni;
    private int[] iAniImgId;
//     private int[] iResImgId;
    private Image[] imgGray;
    //变色
    private int[] iColorChangeValue = null;
    //半透明
    private byte[] byAlphaType = null;
    //半透明值
    private int[] iAlphaValue = null;
    //半透指定颜色数组
    private int[] iAlphaColor = null;
    //当前图片index
    private int iCurrentImgIndex = -1;

    public int getActionId() {
        return this.m_actionID;
    }

    /**
     * 加载动画文件
     * @param is DataInputStream 数据流
     * @throws Exception 读取异常
     */
    private DataInputStream getDataInputStream(String path) {
        try {
            InputStream in = this.getClass().getResourceAsStream(path);
            if (in == null) {
                return null;
            } else {
                return new DataInputStream(in);
            }
        } catch (Exception e) {
            if (Constant.DEBUG) {
                e.printStackTrace();
            }
            return null;
        }
    }

    public boolean loadAnimation(String imgpath, byte type, Image aImage, int pathis) {
        try {
            if(pathis == -1 || pathis == 0)
                return false;
            if (blHaveAni) {
                return true;
            }
            blCanShow = true;
            blHaveAni = true;
            byAnimationType = type;
            iAnimationId = pathis;
            DataInputStream is = getDataInputStream(Constant.RES_ANI_PATH + pathis + ".ani");
            if(is == null)
            {
              blCanShow = false;
               blHaveAni = false;  
               return false;
            }
            //设置图片
            int count, num, size, imgNums;
            Tools.printLog("pathID：" + pathis);
            imgNums = is.readUnsignedByte();
//        strLog  += "imgNums"+imgNums;
            this.imgAni = new Image[imgNums];
            this.iAniImgId = new int[imgNums];
//            this.iResImgId  = new int[imgNums];
            this.iColorChangeValue = new int[imgNums];
            this.byAlphaType = new byte[imgNums];
            for (int i = 0; i < imgNums; i++) {
                this.iAniImgId[i] = -1;
                int imgId = is.readInt();
//                 this.iResImgId[i] = imgId;
                Tools.printLog("玩家图片ID：" + imgId);
                //变色
                this.iColorChangeValue[i] = (short) (is.readInt());
//            strLog  += "imgNums"+ this.iColorChangeValue[i];
                this.byAlphaType[i] = (byte) (is.readUnsignedByte());
//            strLog  += "imgNums"+ this.byAlphaType[i];
                switch (this.byAlphaType[i]) {
                    case 1:
                        //灰度图名字
                        if (imgGray == null) {
                            imgGray = new Image[imgNums];
                        }
                        int imgGrayId = is.readInt();
                        this.imgGray[i] = ResManager.createImage(imgGrayId, type, Constant.RES_PATH, true, -1);
                        break;
                    case 2:
                        if (iAlphaValue == null) {
                            iAlphaValue = new int[imgNums];
                            for (int j = 0; j < imgNums; j++) {
                                iAlphaValue[j] = -1;
                            }
                        }
                        iAlphaValue[i] = (short) (is.readInt());
                        break;
                    case 3:
                        if (iAlphaColor == null) {
                            iAlphaColor = new int[imgNums];
                        }
                        iAlphaColor[i] = Integer.parseInt(is.readUTF(), 16);
                        break;
                }
                //////////测试注掉的
                this.imgAni[i] = ResManager.createImage(imgId, type, imgpath, true, -1);
                if (this.imgAni[i] == null) {
                    this.iAniImgId[i] = imgId;
                    blCanShow = false;
                }
            }
            // 一共有多少个单元图片(比如：一个胳膊，一个头，下半身等图片)，每个动画桢都是由一个或多个单元图片组成
            count = is.readUnsignedShort();
//         is.skip(2);
            // 一个单元图片占四个short，分别代表这个单元图片在素材图上的x,y,width,height
            m_modules = new short[count * EXTEND_PER_MODULE];
//        boolean modIn255 = (is.readByte()==1);
            for (int i = 0; i < count; i++) {
                m_modules[EXTEND_PER_MODULE * i] = (short) (is.readUnsignedByte());
                m_modules[EXTEND_PER_MODULE * i + 1] = (short) (is.readUnsignedShort());
                m_modules[EXTEND_PER_MODULE * i + 2] = (short) (is.readUnsignedShort());
                m_modules[EXTEND_PER_MODULE * i + 3] = (short) (is.readUnsignedShort());
                m_modules[EXTEND_PER_MODULE * i + 4] = (short) (is.readUnsignedShort());
            }
            // 这整个动画有多少动画桢
            count = is.readShort();
            // 创建桢数组，一桢有一个或若干个单元图片组成(比如：用胳膊，头，腿等单元图拼成的整个人物，摆出了某种pose)
            m_frames = new short[count][];
            for (int i = 0; i < count; i++) {
                size = is.readUnsignedByte(); // ！
                num = is.readUnsignedByte(); // 当前桢需要多少个单元图片
//             strLog  += "num"+ num;
                m_frames[i] = new short[num * EXTEND_PER_SPRITE]; // 一个动画桢中的每个单元图片信息占4字节
                for (int j = 0; j < num; j++) {
                    m_frames[i][j * EXTEND_PER_SPRITE] = is.readShort();
                    m_frames[i][j * EXTEND_PER_SPRITE + 1] = is.readShort();
                    m_frames[i][j * EXTEND_PER_SPRITE + 2] = is.readShort();
                    m_frames[i][j * EXTEND_PER_SPRITE + 3] = is.readShort();
                }
            }
            count = is.readUnsignedShort(); // 有多少个动作Action，每个Action动作都是由一个或若干个桢组成
            m_nFrames = new byte[count]; // 为每个Action保存该Action需要用多少个桢，用下标指定Action
            m_actions = new short[count][]; // 保存每个动作的桢序列
            for (int i = 0; i < count; i++) {
                num = is.readUnsignedByte();
                // m_nFrames保存的是每个动作用的桢数量，第i个值表示第i个动作的
                m_nFrames[i] = (byte) num;
                // 保存第i个动作需要用到的桢的索引，以及显示周期，格式：桢在m_frames里的索引下标，周期,顺序存放下去
                m_actions[i] = new short[num * EXTEND_PER_ACTION];
                for (int j = 0; j < m_actions[i].length; j += 2) {
                    m_actions[i][j] = (short) is.readUnsignedShort(); // 贞索引
                    m_actions[i][j + 1] = (short) is.readUnsignedShort(); // 显示周期delay
                }
            }
            is.close();
            is = null;
        } catch (Exception e) {
            blCanShow = false;
            blHaveAni = false;
            if (Constant.DEBUG) {
                e.printStackTrace();
            }
            return false;
        }
        return true;
    }

    private void loadAnimation(byte type, String imgpath, Image aImage, String aniid) throws Exception {
        try {
            blCanShow = true;
            blHaveAni = true;
            iAnimationId = Integer.parseInt(aniid.trim());
            DataInputStream is = getDataInputStream(Constant.RES_PATH + aniid + ".ani");
            //设置图片
            int count, num, size, imgNums;
            imgNums = is.readUnsignedByte();
            this.imgAni = new Image[imgNums];
            this.iAniImgId = new int[imgNums];
//             this.iResImgId = new int[imgNums];
            this.iColorChangeValue = new int[imgNums];
            this.byAlphaType = new byte[imgNums];
            for (int i = 0; i < imgNums; i++) {
                this.iAniImgId[i] = -1;
                int imgId = is.readInt();
//                this.iResImgId[i] = imgId;
                //变色
                this.iColorChangeValue[i] = (short) (is.readInt());
                this.byAlphaType[i] = (byte) (is.readUnsignedByte());
                switch (this.byAlphaType[i]) {
                    case 1:
                        //灰度图名字
                        if (imgGray == null) {
                            imgGray = new Image[imgNums];
                        }

                        this.imgGray[i] = ResManager.createImage(imgId, type, Constant.RES_PATH, true, -1);
//                    fo.writeInt(anim.imgGrayId[i]);
                        break;
                    case 2:
                        if (iAlphaValue == null) {
                            iAlphaValue = new int[imgNums];
                            for (int j = 0; j < imgNums; j++) {
                                iAlphaValue[j] = -1;
                            }
                        }
                        iAlphaValue[i] = (short) (is.readInt());
                        break;
                    case 3:
                        if (iAlphaColor == null) {
                            iAlphaColor = new int[imgNums];
                        }
                        iAlphaColor[i] = Integer.parseInt(is.readUTF(), 16);
                        break;
                }
                this.imgAni[i] = ResManager.createImage(imgId, type, imgpath, true, -1);
                if (this.imgAni[i] == null) {
                    this.iAniImgId[i] = imgId;
                    blCanShow = false;
                }
            }
            // 一共有多少个单元图片(比如：一个胳膊，一个头，下半身等图片)，每个动画桢都是由一个或多个单元图片组成
            count = is.readUnsignedShort();
            // 一个单元图片占四个short，分别代表这个单元图片在素材图上的x,y,width,height
            m_modules = new short[count * EXTEND_PER_MODULE];
//        boolean modIn255 = (is.readByte()==1);
            for (int i = 0; i < count; i++) {
                m_modules[EXTEND_PER_MODULE * i] = (short) (is.readUnsignedByte());
                m_modules[EXTEND_PER_MODULE * i + 1] = (short) (is.readUnsignedShort());
                m_modules[EXTEND_PER_MODULE * i + 2] = (short) (is.readUnsignedShort());
                m_modules[EXTEND_PER_MODULE * i + 3] = (short) (is.readUnsignedShort());
                m_modules[EXTEND_PER_MODULE * i + 4] = (short) (is.readUnsignedShort());
            }
            // 这整个动画有多少动画桢
            count = is.readShort();
            // 创建桢数组，一桢有一个或若干个单元图片组成(比如：用胳膊，头，腿等单元图拼成的整个人物，摆出了某种pose)
            m_frames = new short[count][];
            for (int i = 0; i < count; i++) {
                size = is.readUnsignedByte(); // ！
                num = is.readUnsignedByte(); // 当前桢需要多少个单元图片
                m_frames[i] = new short[num * EXTEND_PER_SPRITE]; // 一个动画桢中的每个单元图片信息占4字节
                for (int j = 0; j < num; j++) {
                    m_frames[i][j * EXTEND_PER_SPRITE] = is.readShort();
                    m_frames[i][j * EXTEND_PER_SPRITE + 1] = is.readShort();
                    m_frames[i][j * EXTEND_PER_SPRITE + 2] = is.readShort();
                    m_frames[i][j * EXTEND_PER_SPRITE + 3] = is.readShort();
                }
            }
            count = is.readUnsignedShort(); // 有多少个动作Action，每个Action动作都是由一个或若干个桢组成
//        strLog  += "count"+ count;
            m_nFrames = new byte[count]; // 为每个Action保存该Action需要用多少个桢，用下标指定Action
            m_actions = new short[count][]; // 保存每个动作的桢序列
            // //保存每个动作的移动信息,每个动作用一个byte型表示，其实只用了它的低4位，从第3位到第0位表示Y方向加速度，X方向加速度，Y方向速度，X方向速度
            // //保存移动信息的具体量值，比如具体的速度值和加速度值
            for (int i = 0; i < count; i++) {
                num = is.readUnsignedByte();
                // m_nFrames保存的是每个动作用的桢数量，第i个值表示第i个动作的
                m_nFrames[i] = (byte) num;
                // 保存第i个动作需要用到的桢的索引，以及显示周期，格式：桢在m_frames里的索引下标，周期,顺序存放下去
                m_actions[i] = new short[num * EXTEND_PER_ACTION];
                for (int j = 0; j < m_actions[i].length; j += 2) {
                    m_actions[i][j] = (short) is.readUnsignedShort(); // 贞索引
                    m_actions[i][j + 1] = (short) is.readUnsignedShort(); // 显示周期delay
                }
            }
            is.close();
            is = null;
        } catch (Exception e) {
            if (Constant.DEBUG) {
                e.printStackTrace();
            }
        }
    }
    //读取网络上的动画文件

    public void loadAnimation(byte type, String imgpath, int aniid, DataInputStream is, boolean blsend) throws Exception {
        try {
            if (blHaveAni) {
                return;
            }
            blCanShow = true;
            iAnimationId = aniid;
             Tools.printLog("netaniid" + aniid);
            //设置图片
            int count, num, size, imgNums;
            imgNums = is.readUnsignedByte();
//        strLog  += "imgNums"+imgNums;
            this.imgAni = new Image[imgNums];
            this.iAniImgId = new int[imgNums];
//            this.iResImgId  = new int[imgNums];
            this.iColorChangeValue = new int[imgNums];
            this.byAlphaType = new byte[imgNums];
            for (int i = 0; i < imgNums; i++) {
                this.iAniImgId[i] = -1;
                int imgId = is.readInt();
                Tools.printLog("netImage" + imgId);
//                 this.iResImgId[i] = imgId;
                //变色
                this.iColorChangeValue[i] = (short) (is.readInt());
                this.byAlphaType[i] = (byte) (is.readUnsignedByte());
                switch (this.byAlphaType[i]) {
                    case 1:
                        //灰度图名字
                        if (imgGray == null) {
                            imgGray = new Image[imgNums];
                        }
                        this.imgGray[i] = ResManager.createImage(is.readInt(), type, Constant.RES_PATH, true, -1);
                        break;
                    case 2:
                        if (iAlphaValue == null) {
                            iAlphaValue = new int[imgNums];
                            for (int j = 0; j < imgNums; j++) {
                                iAlphaValue[j] = -1;
                            }
                        }
                        iAlphaValue[i] = (short) (is.readInt());
                        break;
                    case 3:
                        if (iAlphaColor == null) {
                            iAlphaColor = new int[imgNums];
                        }
                        iAlphaColor[i] = Integer.parseInt(is.readUTF(), 16);
                        break;
                }
                this.imgAni[i] = ResManager.createImage(imgId, type, imgpath, blsend, -1);
                if (this.imgAni[i] == null) {
                    this.iAniImgId[i] = imgId;
                    blCanShow = false;
                }
            }
            // 一共有多少个单元图片(比如：一个胳膊，一个头，下半身等图片)，每个动画桢都是由一个或多个单元图片组成
            count = is.readUnsignedShort();
            // 一个单元图片占四个short，分别代表这个单元图片在素材图上的x,y,width,height
            m_modules = new short[count * EXTEND_PER_MODULE];
            for (int i = 0; i < count; i++) {
                m_modules[EXTEND_PER_MODULE * i] = (short) (is.readUnsignedByte());
                m_modules[EXTEND_PER_MODULE * i + 1] = (short) (is.readUnsignedShort());
                m_modules[EXTEND_PER_MODULE * i + 2] = (short) (is.readUnsignedShort());
                m_modules[EXTEND_PER_MODULE * i + 3] = (short) (is.readUnsignedShort());
                m_modules[EXTEND_PER_MODULE * i + 4] = (short) (is.readUnsignedShort());
            }
            // 这整个动画有多少动画桢
            count = is.readShort();
            // 创建桢数组，一桢有一个或若干个单元图片组成(比如：用胳膊，头，腿等单元图拼成的整个人物，摆出了某种pose)
            m_frames = new short[count][];
            for (int i = 0; i < count; i++) {
                size = is.readUnsignedByte(); // ！
                num = is.readUnsignedByte(); // 当前桢需要多少个单元图片
                m_frames[i] = new short[num * EXTEND_PER_SPRITE]; // 一个动画桢中的每个单元图片信息占4字节
                for (int j = 0; j < num; j++) {
                    m_frames[i][j * EXTEND_PER_SPRITE] = is.readShort();
                    m_frames[i][j * EXTEND_PER_SPRITE + 1] = is.readShort();
                    m_frames[i][j * EXTEND_PER_SPRITE + 2] = is.readShort();
                    m_frames[i][j * EXTEND_PER_SPRITE + 3] = is.readShort();
                }
            }
            count = is.readUnsignedShort(); // 有多少个动作Action，每个Action动作都是由一个或若干个桢组成
            m_nFrames = new byte[count]; // 为每个Action保存该Action需要用多少个桢，用下标指定Action
            m_actions = new short[count][]; // 保存每个动作的桢序列
            // //保存每个动作的移动信息,每个动作用一个byte型表示，其实只用了它的低4位，从第3位到第0位表示Y方向加速度，X方向加速度，Y方向速度，X方向速度
            // //保存移动信息的具体量值，比如具体的速度值和加速度值
            for (int i = 0; i < count; i++) {
                num = is.readUnsignedByte();
                // m_nFrames保存的是每个动作用的桢数量，第i个值表示第i个动作的
                m_nFrames[i] = (byte) num;
                // 保存第i个动作需要用到的桢的索引，以及显示周期，格式：桢在m_frames里的索引下标，周期,顺序存放下去
                m_actions[i] = new short[num * EXTEND_PER_ACTION];
                for (int j = 0; j < m_actions[i].length; j += 2) {
                    m_actions[i][j] = (short) is.readUnsignedShort(); // 贞索引
                    m_actions[i][j + 1] = (short) is.readUnsignedShort(); // 显示周期delay
                }
            }
            is.close();
            is = null;
        } catch (Exception e) {
            if (Constant.DEBUG) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 获得当前动作的下一桢的索引id
     * @param status int[] 状态数组
     * @param continuous boolean 是否循环播放
     */
    int framindex = 0;

    public boolean PlayerOver(int id) {
        return (framindex > this.getFrameNum(id)) ? true : false;
    }

    public void nextFrame(int status[], boolean continuous) {
        if (!blCanShow) {
            return;
        }
        try {
            if (status[4] == 0) {
                this.m_actionOver = true;
            }
            if (m_actions != null && this.m_actionOver && status[1] >= 0 && status[1] < m_actions.length && status[2] < m_actions[status[1]].length) {
                m_durCount = (byte) status[0];
                m_actionID = status[1];
                m_pageID = status[2];
                m_frameIndex = status[3];
                m_actionOver = status[4] == 0 ? true : false;
                m_durCount++;
                framindex++;
                if (m_durCount < m_actions[m_actionID][(m_pageID << 1) + 1]) {
                    // 如果播放当前桢的时间还未达到规定桢数，则继续显示
                    status[0] = m_durCount;
                    status[1] = m_actionID;
                    status[2] = m_pageID;
                    status[3] = m_frameIndex;
                    status[4] = m_actionOver == true ? 0 : 1;
                    return;
                }
                m_pageID++;
                if (m_pageID > m_nFrames[m_actionID] - 1) {
                    m_pageID = 0;
                    framindex = 0;
                    m_actionOver = true;
                    if (!continuous) {
                        m_frameIndex = getFrame(m_actionID, m_pageID);
                        m_durCount = 0;
                        status[0] = m_durCount;
                        status[1] = m_actionID;
                        status[2] = m_pageID;
                        status[3] = m_frameIndex;
                        status[4] = 1;
                        m_actionOver = false;
                        return;
                    }
                }
                // 下一桢的桢索引id给m_frameIndex，在绘制时使用
                m_frameIndex = getFrame(m_actionID, m_pageID);
                m_durCount = 0;
                status[0] = m_durCount;
                status[1] = m_actionID;
                status[2] = m_pageID;
                status[3] = m_frameIndex;
                status[4] = m_actionOver == true ? 0 : 1;
            }
        } catch (Exception e) {
            if (Constant.DEBUG) {
                e.printStackTrace();
            }
        }
    }

    public int getmFrameIndex() {
        return framindex;
    }
    public int iAniX, iAniY = 0;
    public boolean blMirror = false;
    public boolean blApeak = false;

    public void show(Graphics g, int x, int y, int[] status, boolean mirror, boolean apeak) {
        try {
            if (!blCanShow) {
                if (this.iAniImgId == null) {
                    return;
                }
                for (int i = 0; i < this.iAniImgId.length; i++) {
                    if (this.iAniImgId[i] != -1) {
                        this.imgAni[i] = ResManager.getImage(this.iAniImgId[i], (byte) -1);
                        if (this.imgAni[i] != null) {
                            this.iAniImgId[i] = -1;
                            blCanShow = false;
                        } else {
                            return;
                        }
                    }
                }
                blCanShow = true;
                return;
            }
            m_durCount = (byte) status[0];
            m_actionID = status[1];
            m_pageID = status[2];
            m_frameIndex = status[3];
            m_actionOver = status[4] == 0 ? true : false;

            draw(g, x, y, mirror, apeak, m_frameIndex);
            status[0] = m_durCount;
            status[1] = m_actionID;
            status[2] = m_pageID;
            status[3] = m_frameIndex;
            status[4] = m_actionOver == true ? 0 : 1;
        } catch (Exception e) {
            if (Constant.DEBUG) {
                Tools.printLog("aniid:"+iAnimationId);
                e.printStackTrace();
            }
        }
    }

    public void changeAction(int actionID, int pageID, int[] anim_status) {
        if (actionID == -1 || !blHaveAni || m_actions == null || actionID > (this.m_actions.length - 1) || this.m_actions[actionID] == null) {
            return;
        }
        if (actionID > this.m_actions.length || actionID < this.m_actions.length && actionID >= 0 && pageID > this.m_actions[actionID].length || anim_status == null) {
            return;
        }
        //动画周期计时
        anim_status[0] = 0;
        //动作索引
        anim_status[1] = actionID;
        //动作中页索引
        anim_status[2] = pageID;//可以接着动画当前帧播放
        //帧索引
        anim_status[3] = getFrame(actionID, pageID);
        //该动画播放完毕(0未完1完毕)
        anim_status[4] = 0;
    }

    /**
     * 获取指定帧索引
     * @param actionID int 动作索引
     * @param pageID int 动作中的页索引
     * @return int 帧索引
     */
    public int getFrame(int actionID, int pageID) {
        if (actionID < 0 || actionID >= m_nFrames.length) {
            return -1;
        }
        if (pageID < 0 || pageID >= m_nFrames[actionID]) {
            return -1;
        }
        return (m_actions[actionID][pageID << 1]);
    }

    /**
     * 获得当前动作的总帧数
     * @param actionID int 动作id
     * @return int 总帧数
     */
    public final int getFrameNum(int actionID) {
        return actionID >= 0 && actionID < this.m_nFrames.length ? this.m_nFrames[actionID] : 0;
    }

    /**
     * 绘制方法
     * @param g Graphics 画笔
     * @param image Image 图片
     * @param screenX int 绘制在屏幕的x坐标
     * @param screenY int 绘制在屏幕y坐标
     * @param flipX boolean 是否垂直翻转
     * @param flipY boolean 是否水平翻转
     * @param frameIndex int 帧索引
     */
    private void draw(Graphics g, int screenX, int screenY,
            boolean flipX, boolean flipY, int frameIndex) throws Exception {
        if (frameIndex == -1) {
            return;
        }
        int modID, flag, modX, modY, modW, modH, modeImgid;
        short[] s = m_frames[frameIndex];
        int posX, posY, dgflag = 0;
        for (int i = 0; i < s.length; i += 4) {
            modID = (s[i] & 0xFFFF) * 5;
            flag = s[i + 1] & 0xFF;
            modeImgid = m_modules[modID];
            modX = m_modules[modID + 1];
            modY = m_modules[modID + 2];
            modW = m_modules[modID + 3];
            modH = m_modules[modID + 4];
//            Tools.printLog("i:"+i+"modeImgid:"+modeImgid+"modX:"+modX+"modY:"+modY+"modW:"+modW+"modH:"+modH);
            posX = screenX;
            posY = screenY;
            if (flipX) {
                posX -= s[i + 2];
                posY += s[i + 3];
                switch (flag) {
                    case Constant.TRANS_MIRROR_ROT90:
                    case Constant.TRANS_MIRROR_ROT270:
                    case Constant.TRANS_ROT90:
                    case Constant.TRANS_ROT270:
                        flag ^= 0x1;
                        break;
                    default:
                        flag ^= Constant.TRANS_MIRROR;
                        break;
                }
            } else if (flipY) {
                posX += s[i + 2];
                posY -= s[i + 3];
                switch (flag) {
                    case Constant.TRANS_MIRROR_ROT90:
                    case Constant.TRANS_MIRROR_ROT270:
                    case Constant.TRANS_ROT90:
                    case Constant.TRANS_ROT270:
                        flag ^= Constant.TRANS_MIRROR;
                        break;
                    default:
                        flag ^= 0x1;
                        break;
                }
            } else {
                posX += s[i + 2];
                posY += s[i + 3];
            }
            switch (flag) {
                case Constant.TRANS_NONE:
                    break;
                case Constant.TRANS_MIRROR:
                    posX -= modW;
                    break;
                case Constant.TRANS_MIRROR_ROT90:
                    posX -= modH;
                    posY -= modW;
                    break;
                case Constant.TRANS_MIRROR_ROT180:
                    posY -= modH;
                    break;
                case Constant.TRANS_MIRROR_ROT270:
                    break;
                case Constant.TRANS_ROT90:
                    posX -= modH;
                    break;
                case Constant.TRANS_ROT180:
                    posX -= modW;
                    posY -= modH;
                    break;
                case Constant.TRANS_ROT270:
                    posY -= modW;
                    break;
            }
            dgflag = flag;
            g.drawRegion(imgAni[modeImgid], modX, modY, modW, modH, dgflag, posX, posY, Graphics.LEFT | Graphics.TOP);
        }
    }

    /** */
    /**
     * @功能 实现图片的翻转
     * @参数 Image srcImage    原图<br>int transform 翻转方向<br>
     * @返回值 转换后的图片
     * */
    //首先要得到frame宽高
    public static void transformImage(Graphics g, int[] srcImageRgb, int x, int y, int w, int h, int posx, int posy, int transform) {
        int srcW = w;
        int srcH = h;

//        Image imgRet = null;
        switch (transform) {
            case Constant.TRANS_NONE:
//            imgRet = srcImage;
                break;
            case Constant.TRANS_ROT90: {
                for (int i = 0; i < srcH; i++) {
//                    srcImage.getRGB(srcImageRgb, 0, srcW, 0, i, srcW, 1);
                    g.drawRGB(srcImageRgb, 0, 1, srcH - i - 1, 0, 1, srcW, true);
                }
            }
            break;
            case Constant.TRANS_ROT180: {
                for (int i = 0; i < srcH; i++) {
                    arrayExchange(srcImageRgb);
                    g.drawRGB(srcImageRgb, 0, srcW, 0, srcH - 1 - i, srcW, 1, true);
                }
            }
            break;
            case Constant.TRANS_ROT270: {
                for (int i = 0; i < srcH; i++) {
                    arrayExchange(srcImageRgb);
                    g.drawRGB(srcImageRgb, 0, 1, i, 0, 1, srcW, true);
                }
            }
            break;
            case Constant.TRANS_MIRROR: {
                for (int i = 0; i < srcH; i++) {
                    arrayExchange(srcImageRgb);
                    g.drawRGB(srcImageRgb, 0, srcW, 0, i, srcW, 1, true);
                }
            }
            break;
            case Constant.TRANS_MIRROR_ROT90: {
                for (int i = 0; i < srcH; i++) {
                    arrayExchange(srcImageRgb);
                    g.drawRGB(srcImageRgb, 0, 1, srcH - i - 1, 0, 1, srcW, true);
                }
            }
            break;
            case Constant.TRANS_MIRROR_ROT180: {
                for (int i = 0; i < srcH; i++) {
                    g.drawRGB(srcImageRgb, 0, srcW, 0, srcH - 1 - i, srcW, 1, true);
                }
            }
            break;
            case Constant.TRANS_MIRROR_ROT270: {
                for (int i = 0; i < srcH; i++) {
                    g.drawRGB(srcImageRgb, 0, 1, i, 0, 1, srcW, true);
                }
            }
            break;
            default:
//            imgRet = srcImage;
                break;
        }
//        return imgRet;
    }

    /** */
    /**
     * @功能 翻转一个INT数组
     * @参数 要转换的INT数组
     * @返回值    无
     * */
    public static final void arrayExchange(int[] b) {
        int nCount = b.length;
        int nLen = nCount / 2;
        nCount--;
        for (int i = 0; i < nLen; i++) {
            int nTemp = b[i];
            b[i] = b[nCount - i];
            b[nCount - i] = nTemp;
        }
    }
    /*
     * 清理图片
     */
//    public void clearRes(byte type)
//    {
//        for(int i = 0; i <  this.imgAni.length; i++)
//        {
//             ResManager.clearRes(this.iResImgId[i] , type, Constant.RES_TYPE_IMAGE);
//        }
//        this.iResImgId = null;
//    }
}
